<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" disabled placeholder="请输入单据编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单据名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入单据名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="file_type" placeholder="请选择单据类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierId">
              <j-search-select-tag v-model="model.supplierId" dict="erp_supplier,name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="filePath">
              <j-upload v-model="model.filePath"   ></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <a-card title="在线预览" v-if ="model.filePath" >
        <div>
            <div class="pdf-container">
              <iframe :src="handlePreviewFilePDF(model.filePath)" frameborder="0" width="100%" height="500px"></iframe>
            </div>
        </div>
      </a-card>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction, getFileAccessHttpUrl, putAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { Base64 } from 'js-base64'
  import store from '@/store'

  export default {
    name: 'BussMaterialProcurementContractReviewForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          code:""
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: [
            { required: true, message: '请输入单据名称!'},
          ],
        },
        url: {
          add: "/procurement/bussMaterialProcurementContractReview/add",
          edit: "/procurement/bussMaterialProcurementContractReview/edit",
          queryById: "/procurement/bussMaterialProcurementContractReview/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      getSequenceNum() {
        let url = "/sys/fillRule/executeRuleByCode/cghtpsd";
        putAction(url, {}).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            this.model.code = res.result;
          }
        })
      },
      handlePreviewFilePDF(rowpath) {
        let realPath= this.getImgView(rowpath)
        let base = window._CONFIG.onlinePreviewDomainURL + '/onlinePreview?url='
        let codename = Base64.encode(realPath)
        let url = base + codename + '&watermarkTxt=' + encodeURIComponent(store.getters.userInfo.username)
        console.log('预览pdf地址是：'+url)
        return url
      },
      getImgView(text) {
        if (text && text.indexOf(',') > 0) {
          text = text.substring(0, text.indexOf(','))
        }
        return getFileAccessHttpUrl(text)
      },
      add () {
        this.edit(this.modelDefault);
        this.getSequenceNum()
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>